DECLARE	@vDatabaseName	sysname			= 'AdventureWorksDW2008'
,		@vTableName		sysname			= 'DimProductSubCategory'
,		@vSchemaName	sysname			= 'dbo'
,		@vFilterCols	NVARCHAR(MAX)	= 'EnglishProductSubcategoryName'
,		@vFilterCols2	NVARCHAR(MAX)
,		@vSQL			NVARCHAR(MAX)	= '';

SET		@vFilterCols2 = 'CAST([' + REPLACE(@vFilterCols,',','] AS NVARCHAR(MAX)) + '','' + CAST([') + '] AS NVARCHAR(MAX))';
SET		@vSQL = N'SELECT DISTINCT @vFilterCols2 AS FilterVals INTO [FilterVals] FROM [@vDatabasename].[@vSchemaName].[@vTableName];';
SET		@vSQL = REPLACE(REPLACE(REPLACE(REPLACE(@vSQL,'@vFilterCols2',@vFilterCols2),'@vSchemaName',@vSchemaName),'@vTableName',@vTableName),'@vDatabaseName',@vDatabaseName);

DECLARE	_values CURSOR FOR
SELECT	[FilterVals] FROM [FilterVals];

DECLARE	@value NVARCHAR(50)
,		@vNumberOfRows	INT
,		@vCastedValue	NVARCHAR(MAX)
,		@vStartTime		DATETIME2;
DECLARE	@results TABLE (
		[FilterValues]	NVARCHAR(MAX)
,		[NumberOfRows]	INT
,		[TimeToCascade]	INT
);
OPEN	_values;

FETCH NEXT FROM _values
INTO	@value;
WHILE	(@@FETCH_STATUS = 0)
BEGIN
	SET		@vCastedValue = CAST(@value AS NVARCHAR(MAX));
	SET		@vStartTime = SYSDATETIME();
	EXEC	sp_CascadingDataViewer
				@pDatabaseName	=	@vDatabaseName
			,	@pTableName		=	@vTableName
			,	@pSchemaName	=	@vSchemaName
			,	@pNumberOfRows	=	@vNumberOfRows OUTPUT
			,	@pFilterCols	=	@vFilterCols
			,	@pFilterValues	=	@vCastedValue
			,	@pShowData		=	0;
			INSERT	@results ([FilterValues],[NumberOfRows],[TimeToCascade])
			SELECT	@vCastedValue, @vNumberOfRows AS NumberOfRows, DATEDIFF(MS,@vStartTime,SYSDATETIME());
	
	FETCH NEXT FROM _values
	INTO	@value;
END
CLOSE	_values;
DEALLOCATE	_values;

DECLARE	@vTotalRows	INT;
SELECT	@vTotalRows = SUM(r.NumberOfRows)
FROM	@results r;

SELECT	r.[FilterValues]
,		r.[NumberOfRows]
,		CAST(r.NumberOfRows AS DECIMAL(38,5)) / CAST(@vTotalRows AS DECIMAL(38,5)) * 100 AS [PercentOfTotalRows]
,		r.[TimeToCascade]
FROM	@results r
ORDER	BY r.NumberOfRows DESC;